﻿using BootstrapBlazor.Components;
using JinShun.OA.UI.Dtos.Navigation;

namespace JinShun.OA.UI.Extensions
{
    public static class MenuExtensions
    {
        public static async Task<List<MenuItem>> ToMenus(IEnumerable<NavigationOutDto> menus)
        {
            var selectedMenus = new List<MenuItem>();
            foreach (var menuEntity in menus)
            {
                var menuItem = new MenuItem(menuEntity.meta.title!, menuEntity.path, menuEntity.meta.icon);
                if (menuEntity != null && menuEntity.children != null && menuEntity.children.Count > 0)
                {
                    List<MenuItem> menuItems = new List<MenuItem>();
                    foreach (var subitem in menuEntity.children)
                    {
                        var SubmenuItem = new MenuItem(subitem.meta.title!, subitem.path, subitem.meta.icon);
                        menuItems.Add(SubmenuItem);
                    }
                    menuItem.Items = menuItems;
                }
                selectedMenus.Add(menuItem);
            }
            return selectedMenus;
        }
    }
}
